package com.kejiguan.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kejiguan.ui.R;
import com.kejiguan.ui.model.DisplayArea;
import com.kejiguan.ui.model.DisplayMenu;

import java.util.List;

/**
 * Created by apple on 16/3/2.
 */
public class DisplayMenuAdapter extends RecyclerView.Adapter<DisplayMenuVH>{


    public Context mContext;
    private List<DisplayArea> displayAreas;
    private LayoutInflater mInflater;
    private View.OnClickListener onClickListener;
    private DisplayArea selectArea;
    private DisplayArea yuliuArea;

    private int selectColor,unSelectColor;

    public DisplayMenuAdapter(Context mContext, List<DisplayArea> displayAreas) {
        this.mContext = mContext;
        this.displayAreas = displayAreas;
        mInflater=LayoutInflater.from(mContext);

        selectColor=mContext.getResources().getColor(R.color.text_color2);
        unSelectColor=mContext.getResources().getColor(R.color.text_color1);
        if (displayAreas!=null&&displayAreas.size()>0)
        {
            selectArea=displayAreas.get(0);
        }
    }

    @Override
    public DisplayMenuVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView=mInflater.inflate(R.layout.fragment_display_menu_item_layout,null);
        return new DisplayMenuVH(itemView);
    }

    @Override
    public void onBindViewHolder(DisplayMenuVH holder, int position) {

        DisplayArea displayArea=getDisplayAreaByPosition(position);
        holder.menuName.setText(displayArea.getDisplayName());

        if (displayArea.getDisplayId().equals(selectArea.getDisplayId()))
        {
            holder.menuName.setTextColor(selectColor);
            holder.menuName.setBackgroundResource(R.drawable.kjg_item_display_bg2);
        }else
        {
            holder.menuName.setTextColor(unSelectColor);
            holder.menuName.setBackgroundResource(R.drawable.kjg_item_display_bg4);
        }

        if (null!=onClickListener)
        {
            holder.menuName.setTag(R.id.recyclerview_item_position,position);
            holder.menuName.setOnClickListener(onClickListener);
        }

    }

    @Override
    public int getItemCount() {
        if (displayAreas==null)
        {
            return 0;
        }else
        {
            return displayAreas.size()+getYuLiuSize();
        }

    }


    private int getYuLiuSize()
    {
        if (null==yuliuArea)
        {
            return 0;
        }else
        {
            return 1;
        }
    }


    public DisplayArea getDisplayAreaByPosition(int position)
    {
        if (position<displayAreas.size())
        {

            return displayAreas.get(position);

        }else
        {
            return yuliuArea;
        }
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setSelectArea(int position)
    {
        selectArea=getDisplayAreaByPosition(position);
        notifyDataSetChanged();
    }

    public DisplayArea getSelectArea() {
        return selectArea;
    }

    public DisplayArea getYuliuArea() {
        return yuliuArea;
    }

    public void setYuliuArea(DisplayArea yuliuArea) {
        this.yuliuArea = yuliuArea;
    }
}
